<?php
/*

 */
namespace Admin\Controller;
use Admin\Logic\ArticleCatLogic;

class OpenController extends BaseController {
    public function index(){
      $act_list = session('act_list');
      $menu_list = getMenuList($act_list);
      $this->assign('menu_list',$menu_list);
      $admin_info = getAdminInfo(session('admin_id'));
      $this->assign('admin_info',$admin_info);
      //xl
      $first_column=M('first_column');
      $first_column=$first_column->select();
      $second_column=M('second_column');
      $second_column=$second_column->where('first_column_id = 6')->select();
      $third_column=M('third_column');
      $third_column=$third_column->where('first_column_id = 6')->select();
      $this->assign('first_column',$first_column);
      $this->assign('second_column',$second_column);
      $this->assign('third_column',$third_column);
      $this->display();
    }


    /**
     * 初始化编辑器链接
     * @param $post_id post_id
     */
    private function initEditor()
    {
        $this->assign("URL_upload", U('Admin/Ueditor/imageUp',array('savepath'=>'article')));
        $this->assign("URL_fileUp", U('Admin/Ueditor/fileUp',array('savepath'=>'article')));
        $this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp',array('savepath'=>'article')));
        $this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage',array('savepath'=>'article')));
        $this->assign("URL_imageManager", U('Admin/Ueditor/imageManager',array('savepath'=>'article')));
        $this->assign("URL_imageUp", U('Admin/Ueditor/imageUp',array('savepath'=>'article')));
        $this->assign("URL_getMovie", U('Admin/Ueditor/getMovie',array('savepath'=>'article')));
        $this->assign("URL_Home", "");
    }


    public function add_article(){
        $act_list = session('act_list');
        $menu_list = getMenuList($act_list);
        $this->assign('menu_list',$menu_list);
        $admin_info = getAdminInfo(session('admin_id'));
        $this->assign('admin_info',$admin_info);

      if(isset($_POST['province_id']) && $_POST['province_id'] != ''){
        $parent_id=$_POST['province_id'];
        $city=M('region2');
        $result_city=$city->query("select * FROM `xl_region2` where `parent_id` = '$parent_id'");
        $result_city=json_encode($result_city);
    	  echo $result_city;
      }else{
        $get_third_id=$_GET['id'];

        $third_column=M('third_column');
        $third_column=$third_column->query("select * from `xl_third_column` where `id` = '$get_third_id'");

        $fourth_column=M('fourth_column');
        $fourth_column=$fourth_column->query("select * from `xl_fourth_column` where `third_column_id` = '$get_third_id'");

        $first_column_id=$third_column[0]['first_column_id'];
        $first_column=M('first_column');
        $first_column_name=$first_column->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");
        $second_column_id=$third_column[0]['second_column_id'];
        $second_column=M('second_column');
        $second_column_name=$second_column->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");
        $third_column_name=$third_column[0]['name'];

        $province=M('region2');
        $province=$province->where('level = 1')->select();

        $article_total=M('article_xueli');
        $article_total=$article_total->query("select `fourth_column_id` from `xl_article_xueli` where `third_column_id` = '$get_third_id'");
        $time=time();
        $this->assign('third_column',$third_column);
        $this->assign('fourth_column',$fourth_column);
        $this->assign('first_column_name',$first_column_name);
        $this->assign('second_column_name',$second_column_name);
        $this->assign('third_column_name',$third_column_name);
        $this->assign('province',$province);
        $this->assign('article_total',$article_total);
        $this->assign('time',$time);
        $this->initEditor();
        $this->display();
      }

    }

    public function articleList(){
      if(isset($_POST['searchtitle']) && $_POST['searchtitle'] != ''){
        $fourth_column_id=$_POST['fourth_column_id'];
        $article =  M('article_xueli');
        $res = array();
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 100 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $searchtitle = trim(I('searchtitle'));
        $where.=" and title like '%$searchtitle%' and fourth_column_id='$fourth_column_id'";
        $res = $article->where($where)->order('id desc')->page("$p,$size")->select();
        $count = $article->where($where)->count();// 查询满足要求的总记录数
        $pager = new \Think\Page($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数
        $page = $pager->show();//分页显示输出
        // $searchtitle = new ArticleCatLogic();
        // $cats = $ArticleCat->article_cat_list(0,0,false);
        // if($res){
        //   foreach ($res as $val){
        //     $val['category'] = $cats[$val['cat_id']]['cat_name'];
        //     $val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
        //     $list[] = $val;
        //   }
        // }
        // $this->assign('cats',$cats);
        // $this->assign('cat_id',$cat_id);
        // $this->assign('list',$list);// 赋值数据集
        $first_column=M('first_column');
        $first_column_id=$res[0]['first_column_id'];
        $first_column_name=$first_column->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");

        $second_column=M('second_column');
        $second_column_id=$res[0]['second_column_id'];
        $second_column_name=$second_column->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");

        $third_column=M('third_column');
        $third_column_id=$res[0]['third_column_id'];
        $third_column_name=$third_column->query("select `name` from `xl_third_column` where `id` = '$third_column_id'");

        $fourth_column=M('fourth_column');
        $fourth_column_name=$third_column->query("select `name` from `xl_fourth_column` where `id` = '$fourth_column_id'");
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('article',$res);
        $this->assign('get_fourth_column_id',$fourth_column_id);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->assign("third_column_name",$third_column_name);
        $this->assign("fourth_column_name",$fourth_column_name);
        $this->display();
       }else{
        $get_fourth_column_id = $_GET['fourth_column_id'];
        $article =  M('article_xueli');

        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];
        $where = " 1 = 1 ";
        $where.=" and fourth_column_id='$get_fourth_column_id'";
        $res = $article->where($where)->order('id desc')->page("$p,$size")->select();
        $count = $article->where($where)->count();// 查询满足要求的总记录数
        $pager = new \Think\Page($count,$size);// 实例化分页类 传入总记录数和每页显示的记录数
        $page = $pager->show();//分页显示输出

        $first_column=M('first_column');
        $first_column_id=$res[0]['first_column_id'];
        $first_column_name=$first_column->query("select `name` from `xl_first_column` where `id` = '$first_column_id'");

        $second_column=M('second_column');
        $second_column_id=$res[0]['second_column_id'];
        $second_column_name=$second_column->query("select `name` from `xl_second_column` where `id` = '$second_column_id'");

        $third_column=M('third_column');
        $third_column_id=$res[0]['third_column_id'];
        $third_column_name=$third_column->query("select `name` from `xl_third_column` where `id` = '$third_column_id'");

        $fourth_column=M('fourth_column');
        $fourth_column_name=$third_column->query("select `name` from `xl_fourth_column` where `id` = '$get_fourth_column_id'");

        $this->assign('page',$page);// 赋值分页输出
        $this->assign("article",$res);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->assign("third_column_name",$third_column_name);
        $this->assign("fourth_column_name",$fourth_column_name);
        $this->assign("get_fourth_column_id",$get_fourth_column_id);
        $this->display();
      }
    }

    public function edit_article(){
        $act_list = session('act_list');
        $menu_list = getMenuList($act_list);
        $this->assign('menu_list',$menu_list);
        $admin_info = getAdminInfo(session('admin_id'));
        $this->assign('admin_info',$admin_info);

      if(isset($_GET['uid']) && $_GET['uid'] != ''){
        $province=M('region2');
        $province=$province->where('level = 1')->select();

        $article_id=$_GET['uid'];
        $article=M('article_xueli');
        $article=$article->where("id = '$article_id'")->select();
        $article=$article[0];

        $city_id=$article['city_id'];
        $city=M()->query("select * from __PREFIX__region2 where id = '$city_id'");
        $this->assign("city",$city[0]);

        $first_column=M('first_column');
        $first_column_id=$article['first_column_id'];
        $first_column_name=$first_column->where("id = '$first_column_id'")->select();

        $second_column=M('second_column');
        $second_column_id=$article['second_column_id'];
        $second_column_name=$second_column->where("id = '$second_column_id'")->select();

        $third_column=M('third_column');
        $third_column_id=$article['third_column_id'];
        $third_column_name=$third_column->where("id = '$third_column_id'")->select();

        $fourth_column=M('fourth_column');
        $fourth_column_id=$article['fourth_column_id'];
        $fourth_column_name=$fourth_column->where("id = '$fourth_column_id'")->select();

        $this->assign("article",$article);
        $this->assign("province",$province);
        $this->assign("first_column_name",$first_column_name);
        $this->assign("second_column_name",$second_column_name);
        $this->assign("third_column_name",$third_column_name);
        $this->assign("fourth_column_name",$fourth_column_name);
        $this->initEditor();
        $this->display();
      }else{
        $this->error("获取失败",U('Admin/Open/'));
      }
 		   //  $act = I('GET.act','add');
        // $info = array();
        // $info['publish_time'] = time()+3600*24;
        // if(I('GET.article_id')){
        //    $article_id = I('GET.article_id');
        //    $info = D('article')->where('article_id='.$article_id)->find();
        // }
        // $cats = $ArticleCat->article_cat_list(0,$info['cat_id']);
        // $this->assign('cat_select',$cats);
        // $this->assign('act',$act);
        // $this->assign('info',$info);
        // $this->initEditor();
        // $this->display();
    }

    public function articleHandle(){
      $data = I('post.');
      $data['publish_time'] = strtotime($data['publish_time']);
      //$data['content'] = htmlspecialchars(stripslashes($_POST['content']));
      if($data['act'] == 'add'){
         $data['content'] = str_replace('；', ';',$data['content']);
         //$data['click'] = mt_rand(1000,1300);
         $r = D('article_xueli')->add($data);
      }

      if($data['act'] == 'edit'){
          $data['content'] = str_replace('；', ';',$data['content']);
          $r = D('article_xueli')->where('id='.$data['article_id'])->save($data);
      }
      //
      if($data['act'] == 'del'){
        $r = D('article_xueli')->where('id='.$data['article_id'])->delete();
        if($r) exit(json_encode(1));
      }

      $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Open/');
      if($r){
          $this->success("操作成功",$referurl);
      }else{
          $this->error("操作失败",$referurl);
      }
    }
}
